package com.zhumr.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.json.Json;
import javax.json.JsonObject;

import org.codehaus.jackson.map.util.JSONPObject;

/**
 * 短信服务
 * 
 * @author xueyuan
 *
 */
public class SMSUtil {
	/**
	 * 主账号id
	 */
	public static String sid = "8bedbc4ad3f6f416a813ae5d90f78add";
	/**
	 * 应用id
	 */
	public static String appId = "4d44e2ab23324f2cb57485890e291929";
	/**
	 * 短信模板id
	 */
	public static String templateId = "13485";
	/**
	 * token 账户授权令牌
	 */
	public static String token = "a1b0ef62c0bd11a8224db8085c2018cf";
	/**
	 * 请求url
	 */
	public static String url = "http://www.ucpaas.com/maap/sms/code";

	public static void main(String[] args) {
		String phone = "15652388352";
		String number = "123456";
		// System.out.println(sendSMS(phone, number));
		String result = sendSMS(phone, number);
		// System.out.println(get6Number());
	}

	/**
	 * 生成验证码
	 */
	public static String get6Number() {
		Random random = new Random();
		String result = "";
		for (int i = 0; i < 6; i++) {
			result += random.nextInt(10);
		}
		return result;
	}

	/**
	 * 发送短信
	 * 
	 * @param phone
	 *            手机号码
	 * @param to
	 *            内容
	 * @return
	 */
	public static String sendSMS(String phone, String number) {
		String time = getTime();
		String sign = sid + time + token;
		String signMd5 = MD5Util.getMd5(sign);

		String param = "sid=" + sid + "&appId=" + appId + "&time=" + time + "&sign=" + signMd5 + "&to=" + phone
				+ "&templateId=" + templateId + "&param=" + number;

		String result = HttpRequestUtil.sendGet(url, param);
		return result;
	}

	public static String getTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");// 设置日期格式
		return df.format(new Date());
	}

}
